package com.fengplus.demo.controller;

import com.fengplus.demo.entity.AppOrder;
import com.fengplus.demo.entity.AppUser;
import com.fengplus.demo.entity.CommonResult;
import com.fengplus.demo.service.OrderService;
import com.fengplus.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping
    public CommonResult list() {
        List<AppOrder> orderList = orderService.findList();
        return new CommonResult<>(200, "查询成功", orderList);
    }

    @GetMapping("/{id}")
    public CommonResult detail(@PathVariable Long id) {
        List<AppOrder> orderList = orderService.findList();
        return new CommonResult<>(200, "查询成功", orderList);
    }

    @PostMapping
    public CommonResult create(AppOrder order) {
        int isSuccess = orderService.save(order);
        if(isSuccess == 1){
            return new CommonResult<>(200, "操作成功");
        }else{
            return new CommonResult<>(400, "操作失败");
        }
    }

    @PostMapping("/{id}/pay")
    public CommonResult pay(@PathVariable Long id) {
        int isSuccess = orderService.updateStatus(id, 1);
        if(isSuccess == 1){
            return new CommonResult<>(200, "操作成功");
        }else{
            return new CommonResult<>(400, "操作失败");
        }
    }

    @PostMapping("/{id}/cancel")
    public CommonResult cancel(@PathVariable Long id) {
        int isSuccess = orderService.updateStatus(id, -1);
        if(isSuccess == 1){
            return new CommonResult<>(200, "操作成功");
        }else{
            return new CommonResult<>(400, "操作失败");
        }
    }

    @PostMapping("/{id}/delete")
    public CommonResult delete(@PathVariable Long id) {
        int isSuccess = orderService.delete(id);
        if(isSuccess == 1){
            return new CommonResult<>(200, "操作成功");
        }else{
            return new CommonResult<>(400, "操作失败");
        }
    }

}
